package com.game.server.message.proto.execute;

import com.comm.ISession;
import com.game.majiang.config.DataManager;
import com.game.majiang.logic.LogicClient;
import com.game.majiang.logic.LogicServer;
import com.game.server.db.dao.XyClubInfoDao;
import com.game.server.db.dao.XyUnionInfoDao;
import com.game.server.db.entity.XyClubInfo;
import com.game.server.db.entity.XyUnionInfo;
import com.game.server.message.proto.in.AbstractXYunionCreateReq;
import com.game.server.message.proto.out.XYunionCreateRsp;

public class XYunionCreateReq extends AbstractXYunionCreateReq {

    @Override
    public void Execute(ISession session) {
        LogicClient client = (LogicClient) LogicServer.getInstance().getClient(session.getId());
        if (client != null && client.checkClick(msgcode)) {
            XYunionCreateRsp rsp = new XYunionCreateRsp();
            rsp.setIsok(1);
            rsp.setMsg("联盟创建失败");
            if (this.unionName != null && !"".equals(this.unionName)) {
                XyClubInfo xyClubInfo = DataManager.getInstance().getMapClub().get(this.clubid);
                if (xyClubInfo != null && xyClubInfo.getCreatorid() == client.getUserId()) {
                    if (xyClubInfo.getUnionid() == 0) {
                        String pointsRequiredToCreateAnAlliance = DataManager.getInstance().systemValue("pointsRequiredToCreateAnAlliance");
                        if (pointsRequiredToCreateAnAlliance != null && !"".equals(pointsRequiredToCreateAnAlliance) && client.getVo().getIntegral() >= Integer.parseInt(pointsRequiredToCreateAnAlliance)) {
                            String numberOfPeopleNeededToCreateAnAlliance = DataManager.getInstance().systemValue("numberOfPeopleNeededToCreateAnAlliance");
                            if (numberOfPeopleNeededToCreateAnAlliance != null && !"".equals(numberOfPeopleNeededToCreateAnAlliance) && xyClubInfo.getPlayerCount() >= Integer.parseInt(numberOfPeopleNeededToCreateAnAlliance)) {
                                XyUnionInfo unionInfo = new XyUnionInfo();
                                unionInfo.setUnion_head(this.head);
                                unionInfo.setUnion_name(this.unionName);
                                unionInfo.setUnion_integral(0);
                                unionInfo.setCreator_clubid(xyClubInfo.getId());
                                int unionId = XyUnionInfoDao.insertInto(unionInfo);
                                if (unionId != 0) {
                                    client.getVo().AddIntegral(-Integer.parseInt(pointsRequiredToCreateAnAlliance));
                                    rsp.setIsok(0);
                                    unionInfo = XyUnionInfoDao.findById(unionId);
                                    unionInfo.getClubids().add(Integer.valueOf(xyClubInfo.getId()));
                                    xyClubInfo.setUnionid(unionInfo.getId());
                                    XyClubInfoDao.updateUnionId(xyClubInfo.getId(),unionInfo.getId());
                                    DataManager.getInstance().getMapUnion().put(unionInfo.getId(),unionInfo);
                                    rsp.setUnionid(unionInfo.getId());
                                    rsp.setUnionname(unionInfo.getUnion_name());
                                }
                            } else {
                                rsp.setMsg("俱乐部最少拥有" + numberOfPeopleNeededToCreateAnAlliance + "个玩家，才可创建联盟");
                            }
                        } else {
                            rsp.setMsg("创建联盟需要" + pointsRequiredToCreateAnAlliance + "积分");
                        }
                    } else {
                        rsp.setMsg("已有联盟");
                    }
                }
            }
            client.Send(rsp);
        }
    }

}
